package com.yuexunit.operationman.nbsy.module.plant.ui;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.plant.bean.MaterialBean;
import com.yuexunit.operationman.nbsy.module.plant.model.impl.MaterialUpdateModel;
import com.yuexunit.operationman.nbsy.module.plant.presenter.impl.MaterialUpdatePresenter;
import com.yuexunit.operationman.nbsy.module.plant.ui.view.IMaterialUpdateView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/7/11 0011.
 */

public class Act_Plant_Other extends KJActivity implements IMaterialUpdateView {
    @Bind(R.id.scrub_urse)
    EditText scrubUrse;
    @Bind(R.id.circulating_nurse)
    EditText circulatingNurse;
    @Bind(R.id.follow_people)
    EditText followPeople;
    @Bind(R.id.collator)
    EditText collator;
    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;

    MaterialUpdatePresenter updatePresenter;
    LoadingDialog loadingDialog;
    MaterialBean materialBean;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_plant_other);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        setContent();
        loadingDialog = new LoadingDialog(this);
        updatePresenter = new MaterialUpdatePresenter(this, new MaterialUpdateModel());
    }

    private void setTitle()
    {
        tvPatientName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName());
        if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        }else if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientSex().equals("2"))
        {
            tvPatientSex.setText("女");
        }
        else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getInHospitalNo());
        tvPatientDisease.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getOperationName());
    }

    private void setContent() {
        scrubUrse.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getHandNurse());
        circulatingNurse.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getCircuitNurse());
        followPeople.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getAttendantMan());
        collator.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getCheckMan());
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.sure:
                materialBean = AppConfig.MATERIALBEAN;
                materialBean.getObject().getImplantMaterial().setHandNurse(scrubUrse.getText().toString());
                materialBean.getObject().getImplantMaterial().setCircuitNurse(circulatingNurse.getText().toString());
                materialBean.getObject().getImplantMaterial().setAttendantMan(followPeople.getText().toString());
                materialBean.getObject().getImplantMaterial().setCheckMan(collator.getText().toString());
                Map<String, String> params = new HashMap<>();
                params.put("saveData", JSON.toJSONString(materialBean));
                params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
                params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
                params.put("interType", AppConfig.INTERTYPE);
                updatePresenter.UpdateMaterial(params);
                break;
        }
    }


    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

    @Override
    public void updateMaterial(SubmitBean submitBean) {
        if(submitBean.getCode() == 200){
            AppConfig.MATERIALBEAN = materialBean;
            ViewInject.toast("更新成功");
            finish();
        }
    }
}
